<!--#include file="classData.asp"-->
<!--#include file="classSort.asp"-->
<%
Response.Buffer = True 
Response.ExpiresAbsolute = Now() - 1 
Response.Expires = 0 
Response.CacheControl = "no-cache" 
Response.AddHeader "Pragma", "No-Cache"

	tstate = 0

	lps1 = Request.Cookies("LeaguePlanState")
	lps2 = Request.Cookies("LeaguePlanState2")
	
	
	If lps1 <> "" Then

	  Response.Cookies("LeaguePlanState") = ""
	  Response.Cookies("LeaguePlanState1") = ""
	  
	  tstate = 1
	
	End If

	If tstate = 1 Then

	lpid = CInt(Request.QueryString("lpid"))
	maid = CInt(Request.QueryString("maid"))
	

	set HuaObj = new sysData
	HuaObj.sqlConnOpen()

	set SystemRds=server.CreateObject("ADODB.recordset")
	HuaObj.DbQuery SystemRds,"sysVar",QueryStr,0," VarId = 1"
	
	CurrentSeason = SystemRds("CurrentSeason")

	SystemRds.Close


	set LockRds=server.CreateObject("ADODB.recordset")
	HuaObj.DbQuery LockRds,"sysLeagueLock",QueryStr,0," 1=1"
	
	tNum = LockRds.RecordCount
		
	LockRds.Close

	If tNum = 0 Then
	
	InsertSql = "INSERT INTO sysLeagueLock VALUES("&maid&",getdate())"
	HuaObj.SCommand(InsertSql)
	
	End If

	set LockRdsB=server.CreateObject("ADODB.recordset")
	HuaObj.DbQuery LockRdsB,"sysLeagueLock",QueryStr,0," 1=1 ORDER BY OpDate DESC"
	
	tmid = LockRdsB("ManagerId")
		
	LockRdsB.Close

	If tmid = Cint(maid) Then

	HuaObj.p_DbConnection.BeginTrans
	on error resume next

	set ScoreRds=server.CreateObject("ADODB.recordset")
	HuaObj.DbQuery ScoreRds,"LeaguePlan",QueryStr,0," LPId = "&lpid
	
	HomeScore = ScoreRds("HomeScore")
	AwayScore = ScoreRds("AwayScore")
	HomeId = ScoreRds("HomeId")
	AwayId = ScoreRds("AwayId")
	AreaLevel = ScoreRds("AreaLevel")
		
	ScoreRds.Close

	If HomeScore  > AwayScore Then
	
		HScore = 3
		AScore = 0
		HWinNum = 1
		HLostNum = 0
		tHDrawNum = 0
		AWinNum = 0
		ALostNum = 1
		ADrawNum = 0
	
	ElseIf HomeScore  < AwayScore Then
	
		HScore = 0
		AScore = 3
		HWinNum = 0
		HLostNum = 1
		tHDrawNum = 0
		AWinNum = 1
		ALostNum = 0
		ADrawNum = 0
	
	ElseIf HomeScore  = AwayScore Then
		
		HScore = 1
		AScore = 1
		HWinNum = 0
		HLostNum = 0
		tHDrawNum = 1
		AWinNum = 0
		ALostNum = 0
		ADrawNum = 1
		
	End If

	'更新主场
	InsertSql = "UPDATE AreaLeague SET Score = Score + "&HScore&",Goal = Goal + "&HomeScore&",Fumble = Fumble + "&AwayScore&",GoalDif = GoalDif + ("&(HomeScore-AwayScore)&"),TotalRound = TotalRound + 1,WinNum = WinNum + "&HWinNum&",LostNum = LostNum + "&HLostNum&",DrawNum = DrawNum + "&tHDrawNum&" WHERE SeasonNum ="&CurrentSeason&" AND AreaLevel = "&AreaLevel&" AND ManagerId = "&HomeId
	HuaObj.SCommand(InsertSql)
	
	'更新客场
	InsertSql = "UPDATE AreaLeague SET Score = Score + "&AScore&",Goal = Goal + "&AwayScore&",Fumble = Fumble + "&HomeScore&",GoalDif = GoalDif + "&(AwayScore-HomeScore)&",TotalRound = TotalRound + 1,WinNum = WinNum + "&AWinNum&",LostNum = LostNum + "&ALostNum&",DrawNum = DrawNum + "&ADrawNum&" WHERE SeasonNum ="&CurrentSeason&" AND AreaLevel = "&AreaLevel&" AND ManagerId = "&AwayId
	HuaObj.SCommand(InsertSql)
	

	'更新赛程表
	InsertSql = "UPDATE LeaguePlan SET IfDone = 2,ConfirmDate = '"&Date&"' WHERE LPId ="&lpid
	HuaObj.SCommand(InsertSql)
	
	InsertSql = "DELETE FROM sysLeagueLock"
	HuaObj.SCommand(InsertSql)
	
	InsertSql = "INSERT INTO LeagueLog VALUES("&lpid&",'"&lps1&"',"&lps2&",'"&date&" "&time&"',"&maid&",'lc')"
	HuaObj.SCommand(InsertSql)

	response.ContentType="text/xml"	
	Response.Write("<?xml version=""1.0"" encoding=""gb2312""?>"&vbcrlf)
	Response.Write("<xml>"&vbcrlf)


	if err.number =0 then  
    	HuaObj.p_DbConnection.CommitTrans  '如果没有conn错误，则执行事务提交
		call SortSameScore(CurrentSeason,1,AreaLevel,HomeId)
		call SortSameScore(CurrentSeason,1,AreaLevel,AwayId)
		call SortScore(CurrentSeason,1,AreaLevel)
		Response.Write("<Shi ShiId = ""1"">"&vbcrlf)
		Response.Write("</Shi>"&vbcrlf)
	else 
    	HuaObj.p_DbConnection.RollbackTrans '否则回滚
		Response.Write("<Shi ShiId = ""0"">"&vbcrlf)
		Response.Write("</Shi>"&vbcrlf)
	end if

	Else
	
		InsertSql = "DELETE FROM sysLeagueLock WHERE OpDate < DATEADD(n,-3,'"&Date&" "&Time&"')"
		HuaObj.SCommand(InsertSql)
	
		Response.Write("<Shi ShiId = ""2"">"&vbcrlf)
		Response.Write("</Shi>"&vbcrlf)

	End If

	Response.Write("</xml>"&vbcrlf)

	
	HuaObj.sqlConnClose()

	End If
%>
